package green;

import com.sun.xml.internal.fastinfoset.tools.XML_SAX_StAX_FI;

/**
 * @author ST_JG
 * @version 1.0
 */
public class Solution628 {
    public static void main(String[] args) {
        int[] ints = {1, 2, 3,4};
        System.out.println(max(ints));
    }

    static int max(int[] nums){
        int sum =0;
        if(nums.length <= 3){
            sum =1;
            for(int i=0;i<nums.length;i++){
                sum *= nums[i];
            }
            return sum;
        }
        int[] maxs=new int[3];
        for(int i=0;i<nums.length;i++) {
            //初始化，全为正，一旦出现负数就不行
            if (sum == 0) {
                sum = 1;
                if (nums[i] > maxs[2]) {
                    maxs[0] = maxs[1];
                    maxs[1] = maxs[2];
                    maxs[2] = nums[i];
                } else if (nums[i] > maxs[1]) {
                    maxs[0] = maxs[1];
                    maxs[1] = nums[i];
                } else if (nums[i] > maxs[0]) {
                    maxs[0] = nums[i];
                }
                sum = sum * maxs[0] * maxs[1] * maxs[2];
            }
            if (nums[i] > maxs[(i+2) % 3]) {
                maxs[i%3]=nums[i];
            }
        }
        sum = maxs[0] * maxs[1] * maxs[2];
        return sum;
    }

}
